// pages/usercenter/usercenter.js
import axios from '../../utils/axios'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    phoneNumber: '',
    hasUserInfo: false,
    hasMobile: false,
    list: [
      // {
      //   title: '个人资料',
      //   url: '/pages/usercenter/editname/editname',
      //   icon: 'lock-on',
      // },
      // {
      //   title: '修改手机号',
      //   url: '/pages/usercenter/editmobile/editmobile',
      //   icon: 'lock-on',
      // },
      {
        title: '我的会员',
        url: '/pages/usercenter/myvip/myvip',
        icon: 'user-vip',
      },
      {
        title: '充值会员',
        url: '/pages/usercenter/rech/rech',
        icon: 'money',
      },
      {
        title: '消费记录',
        url: '/pages/usercenter/record/record',
        icon: 'sticky-note',
      },
      {
        title: '茶艺师预约记录',
        url: '/pages/usercenter/teaerRecord/record',
        icon: 'tea',
      },
      {
        title: '联系我们',
        url: '/pages/usercenter/contact/contact',
        icon: 'earphone',
      },
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (wx.getStorageSync('userInfo')) {
      this.setData({
        userInfo: wx.getStorageSync('userInfo'),
        hasUserInfo: true
      })
    }
    const memberInfo = wx.getStorageSync('memberInfo')
    if(memberInfo||wx.getStorageSync('phoneNumber')){
      const phoneNumber = wx.getStorageSync('phoneNumber') || JSON.parse(memberInfo).phone
      this.setData({
        phoneNumber: phoneNumber,
        hasMobile: true
      })
    }
  },
  getPhoneNumber(e) {

    if (!this.data.hasMobile) {
      const code = e.detail.code;
      axios.get('/app/getPhoneNumber', {
        code
      }).then(res => {
        this.setData({
          phoneNumber: res.data.phone_info.phoneNumber,
          hasMobile: true
        })
        if(this.data.phoneNumber){
          axios.post("/app/createMember",{
            wxId:wx.getStorageSync('openid'),
            phone:res.data.phone_info.phoneNumber,
          }).then(res=>{
            wx.setStorageSync('memberInfo', JSON.stringify(res.data))
          })
          wx.setStorageSync('phoneNumber', res.data.phone_info.phoneNumber)
        }
      })
    }
  },
  getUserProfile() {
    if (!this.data.hasUserInfo) {
      wx.getUserProfile({
        desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
        success: (res) => {
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
          axios.post("/app/createMember",{
            wxId:wx.getStorageSync('openid'),
            name:res.userInfo.nickName,
            gender:"未知",
          }).then(res=>{
            wx.setStorageSync('memberInfo', JSON.stringify(res.data))
          })
          wx.setStorageSync('userInfo', res.userInfo)
        }
      })
    }
  },
  goToDetail(e) {
    const member = wx.getStorageSync('memberInfo')
    if(!member){
      wx.showToast({
        title: '请先获取手机号和昵称',
        icon:"none"
      })
      return
    }
    const item = e.currentTarget.dataset.item;
    if(item.title === "联系我们"){
      wx.makePhoneCall({
        phoneNumber: '15600595555',
      })
      return;
    }
    wx.navigateTo({
      url: item.url||item,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getTabBar().init();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})